package com.rmt.www.serviceImpl;

import java.util.ArrayList;
import java.util.List;

import com.rmt.www.dao.DesignArtifactDao;
import com.rmt.www.dao.FeatureArtifactDao;
import com.rmt.www.dao.ImplementationArtifactDao;
import com.rmt.www.dao.RequirementArtifactDao;
import com.rmt.www.dao.TestArtifactDao;
import com.rmt.www.dao.TraceabilityDaoV2;
import com.rmt.www.service.AdminManageTraceabilityService;
import com.rmt.www.vo.Artifact;
import com.rmt.www.vo.Cell;
import com.rmt.www.vo.DesignArtifact;
import com.rmt.www.vo.FeatureArtifact;
import com.rmt.www.vo.ImplementationArtifact;
import com.rmt.www.vo.RequirementArtifact;
import com.rmt.www.vo.TestArtifact;
import com.rmt.www.vo.TraceabilityV2;

public class AdminManageTraceabilityServiceImpl implements
		AdminManageTraceabilityService {

	private String fromtable;
	private String totable;
	private List<Artifact> fromList;
	private List<Artifact> toList;
	private FeatureArtifactDao featureArtifactDao;
	private RequirementArtifactDao requirementArtifactDao;
	private DesignArtifactDao designArtifactDao;
	private ImplementationArtifactDao implementationArtifactDao;
	private TestArtifactDao testArtifactDao;
	private TraceabilityDaoV2 traceabilityDaoV2;
	
	
	public String getFromtable() {
		return fromtable;
	}

	public void setFromtable(String fromtable) {
		this.fromtable = fromtable;
	}

	public String getTotable() {
		return totable;
	}

	public void setTotable(String totable) {
		this.totable = totable;
	}

	public List<Artifact> getFromList() {
		return fromList;
	}

	public void setFromList(List<Artifact> fromList) {
		this.fromList = fromList;
	}

	public List<Artifact> getToList() {
		return toList;
	}

	public void setToList(List<Artifact> toList) {
		this.toList = toList;
	}

	public FeatureArtifactDao getFeatureArtifactDao() {
		return featureArtifactDao;
	}

	public void setFeatureArtifactDao(FeatureArtifactDao featureArtifactDao) {
		this.featureArtifactDao = featureArtifactDao;
	}

	public RequirementArtifactDao getRequirementArtifactDao() {
		return requirementArtifactDao;
	}

	public void setRequirementArtifactDao(
			RequirementArtifactDao requirementArtifactDao) {
		this.requirementArtifactDao = requirementArtifactDao;
	}

	public DesignArtifactDao getDesignArtifactDao() {
		return designArtifactDao;
	}

	public void setDesignArtifactDao(DesignArtifactDao designArtifactDao) {
		this.designArtifactDao = designArtifactDao;
	}

	public ImplementationArtifactDao getImplementationArtifactDao() {
		return implementationArtifactDao;
	}

	public void setImplementationArtifactDao(
			ImplementationArtifactDao implementationArtifactDao) {
		this.implementationArtifactDao = implementationArtifactDao;
	}

	public TestArtifactDao getTestArtifactDao() {
		return testArtifactDao;
	}

	public void setTestArtifactDao(TestArtifactDao testArtifactDao) {
		this.testArtifactDao = testArtifactDao;
	}

	public TraceabilityDaoV2 getTraceabilityDaoV2() {
		return traceabilityDaoV2;
	}

	public void setTraceabilityDaoV2(TraceabilityDaoV2 traceabilityDaoV2) {
		this.traceabilityDaoV2 = traceabilityDaoV2;
	}

	
	
	
	
	public void addTraceability(TraceabilityV2 traceabilityV2) {
		traceabilityDaoV2.insertTraceability(traceabilityV2);
	}

	public void deleteTraceability(String fromtable, int fromId, String totable, int toId) {
		traceabilityDaoV2.deleteTraceabilityByFromIdAndToId(fromtable, fromId, totable, toId);
	}

	public List<List<Cell>> getTraceMatrix(String from, String to, int pid){
		fromList=new ArrayList<Artifact>();
		if(from.equals("feature")) {
			fromtable="featureartifact";
			List<FeatureArtifact> featureList=featureArtifactDao.getFeatureArtifactsByPId(pid);
			for(int i=0;i<featureList.size();i++) {
				fromList.add(featureList.get(i));
			}
		}else if(from.equals("requirement")){
			fromtable="requirementartifact";
			List<RequirementArtifact> requirementList=requirementArtifactDao.getRequirementArtifactByPId(pid);
			for(int i=0;i<requirementList.size();i++) {
				fromList.add(requirementList.get(i));
			}
		}else if(from.equals("design")){
			fromtable="designartifact";
			List<DesignArtifact> designList=designArtifactDao.getDesignArtifactsByPId(pid);
			for(int i=0;i<designList.size();i++) {
				fromList.add(designList.get(i));
			}
		}else if(from.equals("implementation")){
			fromtable="implartifact";
			List<ImplementationArtifact> implementationList=implementationArtifactDao.getImplementationArtifactsByPId(pid);
			for(int i=0;i<implementationList.size();i++) {
				fromList.add(implementationList.get(i));
			}
		}else if(from.equals("testcase")){
			fromtable="testartifact";
			List<TestArtifact> testList=testArtifactDao.getTestArtifactsByPId(pid);
			for(int i=0;i<testList.size();i++) {
				fromList.add(testList.get(i));
			}
		}
		
		toList=new ArrayList<Artifact>();
		if(to.equals("feature")) {
			totable="featureartifact";
			List<FeatureArtifact> featureList=featureArtifactDao.getFeatureArtifactsByPId(pid);
			for(int i=0;i<featureList.size();i++) {
				toList.add(featureList.get(i));
			}
		}else if(to.equals("requirement")){
			totable="requirementartifact";
			List<RequirementArtifact> requirementList=requirementArtifactDao.getRequirementArtifactByPId(pid);
			for(int i=0;i<requirementList.size();i++) {
				toList.add(requirementList.get(i));
			}
		}else if(to.equals("design")){
			totable="designartifact";
			List<DesignArtifact> designList=designArtifactDao.getDesignArtifactsByPId(pid);
			for(int i=0;i<designList.size();i++) {
				toList.add(designList.get(i));
			}
		}else if(to.equals("implementation")){
			totable="implartifact";
			List<ImplementationArtifact> implementationList=implementationArtifactDao.getImplementationArtifactsByPId(pid);
			for(int i=0;i<implementationList.size();i++) {
				toList.add(implementationList.get(i));
			}
		}else if(to.equals("testcase")){
			totable="testartifact";
			List<TestArtifact> testList=testArtifactDao.getTestArtifactsByPId(pid);
			for(int i=0;i<testList.size();i++) {
				toList.add(testList.get(i));
			}
		}
		
		
		List<List<Cell>> matrix=new ArrayList<List<Cell>>();
		List<Cell> row=new ArrayList<Cell>();
		
		//增加第一行
		row.add(new Cell(-1,-1,"->", fromtable, totable));
		int toListSize=toList.size();
		for(int i=0;i<toListSize;i++){
			row.add(new Cell(-1,-1,toList.get(i).getTitle(), fromtable, totable));
		}
		matrix.add(row);
		
		//增加后面的行
		Artifact arti;
		int fromListSize=fromList.size();
		int fromId,toId;
		boolean result;
		for(int i=0;i<fromListSize;i++){
			row=new ArrayList<Cell>();
			arti=fromList.get(i);
			fromId=arti.getId();
			row.add(new Cell(-1,-1,arti.getTitle(), fromtable, totable));
			
			//下面判断是否有依赖关系并设置
			for(int j=0;j<toListSize;j++){
				toId=toList.get(j).getId();
				result=traceabilityDaoV2.determinDependencyByFromAndToId(fromtable, fromId, totable, toId);
				if(result==true){
					row.add(new Cell(fromId,toId,"X",fromtable,totable));
				}else{
					row.add(new Cell(fromId,toId,"",fromtable,totable));
				}
			}
			matrix.add(row);
		}
		
		return matrix;
		
	}
}
